﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace intelliSys.Agility.OpenXml
{
    public static class ValHelper
    {
        /// <summary>
        /// This method swallows all exceptions. Even if object o does not have a definition for val
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool TryGetValue<T>(object o, out T value)
        {
            value = default(T);
            try
            {
                var prop = o.GetType().GetProperty("Val");
                var valueWrapper = prop.GetValue(o, null); 
                value = (T)valueWrapper.GetType().GetProperty("Value").GetValue(valueWrapper, null);
                return true;

            }
            catch
            {
                return false;
            }
        }
    }
}
